<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace custom;

/**
 * Description of Jiyuan
 *
 * @author wang
 */
class Jiyuan {

    /**
     * 获取短信验证码
     * @param type $mobiles     手机号码列表，多个号码以“;”分隔
     * @param type $sendTime    定时发送时间：yyyy-MM-dd HH:mm:ss，传空字符串为立即发送
     * @param type $smsContent  短信内容
     * @return type
     */
    public function sendMessage($mobiles, $smsContent, $sendTime=""){
        $service = "sms";
        $method = "sendSms";
        $channel = 1;// 短信通道：0亿美；1建周
        $result = $this->requestSMSData($service, $method, $channel, $mobiles, $sendTime, urlencode($smsContent));
        if($result >= 0){
            return 1;
        } else {
            return $result;
        }
    }
    
    /**
     * 请求极元短信接口数据
     * @param array $data
     * @return bool|mixed|string
     */
    private function requestSMSData($service, $method, $channel, $mobiles, $sendTime, $smsContent){
        $result = false;
        if(!empty($service) && !empty($method)){
            $url = JIYUAN_API . $service . "/?WSDL";//接口路径
            $p['auth'] = 'zy';
            $p['authCode'] = '4e4823f47882470a8fe861e9309ab416';
            $p['channel'] = $channel;
            $p['mobiles'] = $mobiles;
            $p['sendTime'] = $sendTime;
            $p['smsContent'] = json_encode($smsContent);
            
            $client = new \soapclient($url);
            $client->decode_utf8 = false;
            $client->soap_defencoding = 'utf-8'; //java接收是是使用utf-8的，设置为utf-8，防止出现乱码
            $return = $client->__soapCall($method, array($p));
            $result = urldecode($return->return);
            $result = str_replace("is_success", 'isSuccess', $result);
        }
        return $result;
    }
}
